package com.example.final_project.Comm.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.final_project.Comm.Activity.CarActivity;
import com.example.final_project.Comm.entry.CommGuild;
import com.example.final_project.R;

import java.util.List;

public class GuildAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<CommGuild> guilds;

    public GuildAdapter(Context context,List<CommGuild> guilds){
        this.context = context;
        this.guilds = guilds;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.comm_guild_kid,parent,false);
        GKidsHolder gKidsHolder = new GKidsHolder(view);
        return gKidsHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        GKidsHolder gKidsHolder = (GKidsHolder)holder;
        gKidsHolder.setData(position);
    }

    @Override
    public int getItemCount() {
        return guilds.size();
    }

    class GKidsHolder extends RecyclerView.ViewHolder {
        public GKidsHolder(@NonNull View itemView) {
            super(itemView);
        }
        public void setData(int position){
            ImageView car = itemView.findViewById(R.id.comm_guild_IV);
            Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+guilds.get(position).getImg()).into(car);
            TextView name = itemView.findViewById(R.id.comm_guild_TV);
            name.setText(guilds.get(position).getName());
            Button button = itemView.findViewById(R.id.comm_guild_BT);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, CarActivity.class);
                    intent.putExtra("name",guilds.get(position).getName());
                    context.startActivity(intent);
                }
            });
        }
    }

}
